package com.supergdtop.www.run.tools;

import android.annotation.SuppressLint;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 时间工具类
 *
 * @author Asum
 */
@SuppressLint("SimpleDateFormat")
public class TimeTools {
    /**
     * 获取当前是星期几
     *
     * @return
     */
    public static int getWeekNow() {
        int[] weekDays = {7, 1, 2, 3, 4, 5, 6};
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());

        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0) {
            w = 0;
        }

        return weekDays[w];
    }

    /**
     * 获取现在时间与传入时间相差的天数
     *
     * @param time
     * @param format
     * @return
     */
    public static int getTheSpaceByTimeAndFormat(String time, String format) {
        int day = 31;
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            Date d1 = new Date();
            Date d2 = sdf.parse(time);
            Calendar cal = Calendar.getInstance();

            cal.setTime(d1);
            cal.set(Calendar.HOUR_OF_DAY, 0);
            cal.set(Calendar.MINUTE, 0);
            d1 = cal.getTime();
            long time1 = cal.getTimeInMillis();

            cal.setTime(d2);
            cal.set(Calendar.HOUR_OF_DAY, 0);
            cal.set(Calendar.MINUTE, 0);
            d2 = cal.getTime();
            long time2 = cal.getTimeInMillis();

            day = (int) Math.rint((time2 - time1) / (double) (1000 * 3600 * 24));
            return day;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return day;
    }

    /**
     * 获取传入的时间是否为已过去的时间
     *
     * @param time
     * @param format
     * @return
     */
    public static boolean isLostTime(String time, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            Date d1 = new Date();
            Date d2 = sdf.parse(time);
            Calendar cal = Calendar.getInstance();

            cal.setTime(d1);
            cal.set(Calendar.HOUR_OF_DAY, 0);
            cal.set(Calendar.MINUTE, 0);
            d1 = cal.getTime();
            long time1 = cal.getTimeInMillis();

            cal.setTime(d2);
            cal.set(Calendar.HOUR_OF_DAY, 0);
            cal.set(Calendar.MINUTE, 0);
            d2 = cal.getTime();
            long time2 = cal.getTimeInMillis();

            double day = Math.rint((time2 - time1) / (double) (1000 * 3600 * 24));
            if (day >= 0) {
                return false;
            } else {
                return true;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 将指定时间转成指定格式返回
     *
     * @param time
     * @param oldFormat
     * @param newFormat
     * @return
     */
    public static String changeTimeFormat(String time, String oldFormat, String newFormat) {
        SimpleDateFormat sdf = new SimpleDateFormat(oldFormat);
        try {
            Date date = sdf.parse(time);

            SimpleDateFormat formatter = new SimpleDateFormat(newFormat);
            String dateString = formatter.format(date);
            return dateString;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "未知错误";
    }

    /**
     * 判断指定的时间是否为周末
     *
     * @param time
     * @param format
     * @return
     */
    public static boolean isWeekend(String time, String format) {
        int[] weekDays = {7, 1, 2, 3, 4, 5, 6};

        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            Date date = sdf.parse(time);

            Calendar cal = Calendar.getInstance();
            cal.setTime(date);

            int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
            if (w < 0) {
                w = 0;
            }

            if (weekDays[w] == 6 || weekDays[w] == 7) {
                return true;
            } else {
                return false;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 获取当前时间，根据指定格式
     *
     * @param format
     * @return
     */
    public static String getNowTimeByFormat(String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        String dateString = formatter.format(new Date());
        return dateString;
    }

    /**
     * 获取当前时间
     *
     * @return
     */
    public static long getNowTimeFromLong() {
        return new Date().getTime();
    }

    /**
     * 获取现在时间与传入时间相差的天数
     *
     * @param createTime
     * @return
     */
    public static int getTheSpaceByTime(long createTime) {
        int day = 31;
        Date d1 = new Date();
        Date d2 = new Date(createTime);
        Calendar cal = Calendar.getInstance();

        cal.setTime(d1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        d1 = cal.getTime();
        long time1 = cal.getTimeInMillis();

        cal.setTime(d2);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        d2 = cal.getTime();
        long time2 = cal.getTimeInMillis();

        day = (int) Math.rint((time2 - time1) / (double) (1000 * 3600 * 24));
        return day;
    }

    /**
     * 将指定时间转换成指定格式的字符串
     *
     * @param time
     * @param format
     * @return
     */
    public static String getTimeByFormat(long time, String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        String dateString = formatter.format(new Date(time));
        return dateString;
    }

    /**
     * 时间转换
     *
     * @param time
     * @return
     */
    public static String getStringTime(long time) {
        Date nowDate = new Date();
        Date dat = new Date(time);

        long length = getNowTimeFromLong() - time;
        if (length < 60000) {
            return "刚刚";
        }
        if (nowDate.getDate() == dat.getDate() && nowDate.getMonth() == dat.getMonth()) {
            GregorianCalendar gc = new GregorianCalendar();
            gc.setTime(dat);
            SimpleDateFormat simpleformat = new SimpleDateFormat("HH:mm");
            return simpleformat.format(gc.getTime());
        }

        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(dat);
        SimpleDateFormat simpleformat = new SimpleDateFormat("yyyy-MM-dd");
        return simpleformat.format(gc.getTime());
    }

    /**
     * 获取当前是第几月
     *
     * @return
     */
    public static int getMonthNow() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());

        int month = cal.get(Calendar.MONTH) + 1;
        return month;
    }

    /**
     * 获取指定年月日是星期几
     *
     * @param year
     * @param month
     * @param day
     * @return
     */
    public static int getWeekByYearMonthAndDay(int year, int month, int day) {
        int[] weekDays = {7, 1, 2, 3, 4, 5, 6};
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month - 1);
        cal.set(Calendar.DAY_OF_MONTH, day);

        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0) {
            w = 0;
        }

        return weekDays[w];
    }

    /**
     * 获取指定年月最大天数
     *
     * @param year
     * @param month
     * @return
     */
    public static int getMaxDayByYearAndMonth(int year, int month) {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR, year);
        //设置月份
        cal.set(Calendar.MONTH, month - 1);
        //获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);

        return lastDay;
    }

    /**
     * 获取指定年月跨过了几个星期
     *
     * @param year
     * @param month
     * @return
     */
    public static int getWeeksByYearAndMonth(int year, int month) {
        int maxDay = getMaxDayByYearAndMonth(year, month);
        int startWeek = getWeekByYearMonthAndDay(year, month, 1);

        int maxLine = 1;
        maxDay = maxDay - (7 - startWeek + 1);
        maxLine += maxDay / 7;

        maxDay = maxDay % 7;
        if (maxDay > 0) {
            maxLine++;
        }

        return maxLine;
    }

    /**
     * 获取某年某月第几行（最多6行）的所有日期
     *
     * @param year  某年
     * @param month 某月
     * @param row   某行(1~6)
     * @return 所有日期集合
     */
    public static ArrayList<Integer> getAllDayNumByDateAndRow(int year, int month, int row) {
        ArrayList<Integer> days = new ArrayList<Integer>();
        int maxDay = getMaxDayByYearAndMonth(year, month);
        int startWeek = getWeekByYearMonthAndDay(year, month, 1);

        if (row == 1) {
            for (int i = 1; i < startWeek; i++) {
                days.add(0);
            }
            for (int i = startWeek; i < 8; i++) {
                days.add(i - startWeek + 1);
            }
        } else {
            for (int i = 0; i < 7; i++) {
                int num = 7 - startWeek + 1 + (row - 2) * 7 + i + 1;
                if (num > maxDay) {
                    days.add(0);
                } else {
                    days.add(num);
                }
            }
        }
        return days;
    }

    /**
     * 获取指定时间是当年的第几周
     *
     * @param year
     * @param month
     * @param day
     * @return
     */
    public static int getWeekOfYear(int year, int month, int day) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month - 1);
        cal.set(Calendar.DAY_OF_MONTH, day);
        int weekOfYear = cal.get(Calendar.WEEK_OF_YEAR);

        if (getWeekByYearMonthAndDay(year, month, day) == 7) {
            weekOfYear--;
        }

        return weekOfYear;
    }

    /**
     * 获取指定时间是第几年
     *
     * @param time
     * @return
     */
    public static int getYearByTime(long time) {
        Date dat = new Date(time);
        Calendar cal = Calendar.getInstance();
        cal.setTime(dat);

        int year = cal.get(Calendar.YEAR);
        return year;
    }

    /**
     * 获取指定时间是第几月
     *
     * @param time
     * @return
     */
    public static int getMonthByTime(long time) {
        Date dat = new Date(time);
        Calendar cal = Calendar.getInstance();
        cal.setTime(dat);

        int month = cal.get(Calendar.MONTH) + 1;
        return month;
    }

    /**
     * 获取指定时间是第几号
     *
     * @param time
     * @return
     */
    public static int getDayByTime(long time) {
        Date dat = new Date(time);
        Calendar cal = Calendar.getInstance();
        cal.setTime(dat);

        int day = cal.get(Calendar.DAY_OF_MONTH);
        return day;
    }

    /**
     * 获取指定时间是第几小时
     *
     * @param time
     * @return
     */
    public static int getHourByTime(long time) {
        Date dat = new Date(time);
        Calendar cal = Calendar.getInstance();
        cal.setTime(dat);

        int hour = cal.get(Calendar.HOUR_OF_DAY);
        return hour;
    }

    /**
     * 获取指定时间是第几分钟
     *
     * @param time
     * @return
     */
    public static int getMinByTime(long time) {
        Date dat = new Date(time);
        Calendar cal = Calendar.getInstance();
        cal.setTime(dat);

        int min = cal.get(Calendar.MINUTE);
        return min;
    }

    /**
     * 获取指定时间的年份
     *
     * @param time
     * @param format
     * @return
     */
    public static int getYearByTime(String time, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            Date date = sdf.parse(time);

            Calendar cal = Calendar.getInstance();
            cal.setTime(date);

            return cal.get(Calendar.YEAR);
        } catch (ParseException e) {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 获取指定时间的月份
     *
     * @param time
     * @param format
     * @return
     */
    public static int getMonthByTime(String time, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            Date date = sdf.parse(time);

            Calendar cal = Calendar.getInstance();
            cal.setTime(date);

            return cal.get(Calendar.MONTH) + 1;
        } catch (ParseException e) {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 获取指定时间的天
     *
     * @param time
     * @param format
     * @return
     */
    public static int getDayByTime(String time, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            Date date = sdf.parse(time);

            Calendar cal = Calendar.getInstance();
            cal.setTime(date);

            return cal.get(Calendar.DAY_OF_MONTH);
        } catch (ParseException e) {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 获取指定时间的小时
     *
     * @param time
     * @param format
     * @return
     */
    public static int getHourByTime(String time, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            Date date = sdf.parse(time);

            Calendar cal = Calendar.getInstance();
            cal.setTime(date);

            return cal.get(Calendar.HOUR_OF_DAY);
        } catch (ParseException e) {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 获取指定时间的分钟
     *
     * @param time
     * @param format
     * @return
     */
    public static int getMinByTime(String time, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            Date date = sdf.parse(time);

            Calendar cal = Calendar.getInstance();
            cal.setTime(date);

            return cal.get(Calendar.MINUTE);
        } catch (ParseException e) {
            e.printStackTrace();
            return 0;
        }
    }
}
